package com.sne.airways.basecore.filters;

import com.sne.airways.basecore.utils.CommonUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: 卞京帅{bjslogin@gmail.com}
 * Date: 12-11-11
 * Time: 下午3:51
 * 绑定此次请求的一些对象
 */
public class CommonFilter implements Filter {

    private FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;

        String url = StringUtils.substringBefore(StringUtils.substringAfter(request.getRequestURI(), request.getContextPath()),"?");
        if(url.indexOf(".") > 0 && !url.endsWith(".jsp")){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        CommonUtils.setRequest((HttpServletRequest) servletRequest);
        CommonUtils.setResponse((HttpServletResponse)servletResponse);
        CommonUtils.setServletContext(filterConfig.getServletContext());
        filterChain.doFilter(servletRequest,servletResponse);
    }

    public void destroy() {
        CommonUtils.removeCommon();
    }
}
